#!/usr/local/bin/python2.4
# -*- coding: UTF-8 -*-
# visualisering för klubb melt
# kristoffer josefsson 2005

from vj import *
from texturegl import *
from shader import *
from OpenGL.GLU import *
from LSystemGL import *
from bridge import BridgeCubeRecreator

import flickr
import urllib

from random import *
import string

class Bridge(Scene):
	def	__init__(self):
		Scene.__init__(self)
		
		self.title = "schmelting accident"
		self.fov = 40

	def	init(self, width, height):
	
		Scene.init(self, width, height)
	
	#	self.initLighting()	
		
		self.back = ZBillboard("zbuffer.png")
		self.plane = ImageBillboard("tummensvartvitstor.png")
		self.bill = RecreatingCube(BridgeCubeRecreator,1)
		
		self.lsgl = LSystemGL(uniform(0.6,2),uniform(-0.4,0.5))
				
		self.sphere = gluNewQuadric()		
		self.shader = MyMaterial("sphere.glsl")

		glEnable(GL_DEPTH_TEST)

		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()					# reset	the	projection matrix
		gluPerspective(self.fov, float(width)/float(height), 0.1, 100.0)
						# calculate	aspect ratio of	window
		glMatrixMode(GL_MODELVIEW)


	def	display(self):
		Scene.display(self)
		
		for side in self.bill.sides:
			side.offset = self.rtri/500

	#	self.back.draw()
		
		glTranslatef(-0.5,-0.5,-1)
		glColor4f(0.3,1.0,0.3,0.4)
		glDepthFunc(GL_LESS)
		glDisable(GL_DEPTH_TEST)
		glEnable(GL_BLEND)
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA)
		glEnable(GL_LIGHTING)

		glPushMatrix()
	#	glTranslatef(0,0,2*sin(self.rtri))
		self.bill.draw()
		glPopMatrix()
	
		glRotatef(self.rtri*5, 1,1,1)

		self.shader.activate()

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"n")
		glUniform1fARB(self.loc1,3.0)

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"LightPosition")
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"SurfaceColor")		glUniform3fARB(self.loc1,1.0,1.0,1.0)		glUniform3fARB(self.loc2,1.0,0.2,0.2)	

		self.loc1 = glGetUniformLocationARB(self.shader.m_program,"Radius")
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"Blend")		glUniform1fARB(self.loc1,sin(0.1*self.rtri+1.0))		glUniform1fARB(self.loc2,cos(self.rtri))		
		
		glPushMatrix()
		glLoadIdentity()
		glTranslate(0,0,-2)
		gluSphere(self.sphere,0.2,128,128)
		glPopMatrix()
		
		self.loc2 = glGetUniformLocationARB(self.shader.m_program,"SurfaceColor")		glUniform3fARB(self.loc2,0.5,0.5,1.0)	
		
		glPushMatrix()
		glLoadIdentity()
		glTranslate(0.0,0.0,-2)
	#	self.lsgl.draw()
		self.shader.deactivate()
		self.lsgl.draw()
		glPopMatrix()
		
		self.lsgl.update(sin(self.rtri/5))
	#	self.simplex.update(self.rtri)

		glutSwapBuffers()


	def	keyboard(self, key,	x, y):
		Scene.keyboard(self, key, x,y)
		
		if key in ['1','2','3','4','5','6']:
			self.bill.permutate(string.atoi(key))
		
		if key == 'r':
			self.init(640,480)
			self.bill.recreate()
		
		if key == 'o':
			for side in self.bill.sides:
				side.offset -= 0.01
		
		if key == 'O':
			for side in self.bill.sides:
				side.offset += 0.01

		if key == 'h':
			for side in self.bill.sides:
				side.height -= 0.1
		
		if key == 'H':
			for side in self.bill.sides:
				side.height += 0.1
			
		if key == 's':
			for side in self.bill.sides:
				side.softness -= 0.1
		
		if key == 'S':
			for side in self.bill.sides:
				side.softness += 0.1
if __name__=="__main__":
	
#	try:
#        	import psyco
#		psyco.full()
#   	except:
# 	     	print 'Psyco not found, ignoring it'


	s=Bridge()
	launchConsole()
	s.main(sys.argv)
	
